<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2016/4/21
 * Time: 11:36
 */
namespace wap\controllers;
use services\models\EggsGame;
use services\models\MemberInfo;
use services\models\MemberScore;
use yii;
use services\models\ClockIn;

class UserCenterController extends CustomController{
    
    public function actionIndex() {
        if(!$this->isLogin()){
            //用户未登录，跳转至登录页面
            return $this->redirect($this->login_url);
        }
        $lkt_uid = $this->liketry_uid;
        $info = MemberInfo::find()->where(['member_no'=>$lkt_uid])->one();
        if($lkt_uid < 1 || empty($info)){
            //用户不存在或未登录，跳转至登录页面
            return $this->redirect($this->login_url);
        }
        $data['lkt_uid'] = $lkt_uid;
        $data['is_bind'] = $info->checkCell();//手机是否绑定，已绑定：true，未绑定：false
        $data['user_money'] = $info->getMoney();//获取账户余额
        $data['user_score'] = MemberScore::getScore($lkt_uid);//获取用户试豆数
        $data['username'] = MemberInfo::getUserNameByMemberId($lkt_uid);//获取用户昵称
        $data['is_DaKa'] = ClockIn::is_DaKa($lkt_uid);//今日是否打过卡，已打卡：true，未打卡：false
        $data['imgSrc'] = MemberInfo::getAvatarByMemberId($lkt_uid);//用户头像

        return $this->render('index.php',[
            'data'  => $data
        ]);

    }

    /*
     * 打卡
     * */
    public function actionClockIn(){
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $lkt_uid = $this->liketry_uid;
        $Model = new ClockIn();
        if(!$Model->is_DaKa($lkt_uid)){
            if($Model->DaKa($lkt_uid)){
                return ['flag'=>true, 'msg'=>'打卡成功'];
            }
            else{
                return ['flag'=>false, 'msg'=>'打卡失败，请稍后重试'];
            }
        }
        return ['flag'=>true, 'msg'=>'今日已打卡'];
    }


    /*
     * 若用户未绑定手机，跳转至绑定手机页面
     * 若已绑定手机，跳转至解绑提示页面,wap端不提供手机解绑功能
     * */
    public function actionToCell(){
        if(!$this->isLogin()){
            //用户未登录，跳转至登录页面
            return $this->redirect($this->login_url);
        }
        $lkt_uid = $this->liketry_uid;
        $model = MemberInfo::find()->where(['member_no'=>$lkt_uid])->one();
        if($lkt_uid < 1 && empty($model)){
            //用户未登录或不存在，跳转至登录页面
            return $this->redirect($this->login_url);
        }
        $is_bind = $model->checkCell();
        if($is_bind){//已绑定，转至解绑提示页面
            $cellShow = substr_replace($model->cell,'****', 3, 4);
            return $this->render('cancel_phone',[
                'cell'  => $cellShow
            ]);
        }
        else{//未绑定，转至手机绑定页面
            return $this->render('bind_phone');
        }

    }

    /*
     * 发送验证码
     * */
    public function actionSendCode(){

        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $cell = Yii::$app->request->post('cell');
        if(empty($cell) || strlen($cell) != 11 || !is_numeric($cell) ){
            return ['flag'=>false, 'msg'=>'请输入正确的手机号！', 'type'=>1];
        }
        $lkt_uid = $this->liketry_uid;

        $model = MemberInfo::find()->where(['member_no'=>$lkt_uid])->one();
        if(empty($model)){//检测用户是否存在
            return ['flag'=>false, 'msg'=>'请先登录！', 'type'=>0];
        }
        if($model->checkCell()){//检测用户是否绑定过手机号码
           return ['flag'=>false, 'msg'=>'您已绑定过手机号码', 'type'=>0];
        }
        $cellModel = MemberInfo::find()->where(['cell'=>$cell])->one();
        if(!empty($cellModel)){//检测手机号码是否被绑定过
            return ['flag'=>false, 'msg'=>'该手机号码已被绑定', 'type'=>1];
        }
        if($model->cellConfirmProc($cell)){
            return ['flag'=>true, 'msg'=>'验证码已发送', 'type'=>0];
        }
        else{
            return ['flag'=>false, 'msg'=>'发送失败，请不要频繁点击发送且保持手机畅通!', 'type'=>0];
        }
    }

    /*
     * 绑定手机
     * */
    public function actionBindCell(){

        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $cell = Yii::$app->request->post('cell');
        $code = Yii::$app->request->post('code');
        $lkt_uid = $this->liketry_uid;
        //$lkt_uid = 1556685;
        if(empty($cell) || empty($code) || strlen($cell) < 11){
            return ['flag'=>false, 'msg'=>'参数错误'];
        }
        $model = MemberInfo::find()->where(['member_no'=>$lkt_uid])->one();
        if(!empty($model)){
            if($model->cellConfirmProc($cell, $code)){
                return ['flag'=>true, 'msg'=>'绑定成功！'];
            }
            else{

                $tip = $model->getErrors('cell');
                return ['flag'=>false, 'msg'=>$tip];
            }
        }
        return ['flag'=>false, 'msg'=>'请先登录！'];
    }



    /*转至砸金蛋游戏页面*/
    public function actionToEggsGame(){

        return $this->render('egg_game.php');
    }
    /*砸金蛋*/
    public function actionToLottery(){
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $lkt_uid = $this->liketry_uid;
        if($lkt_uid < 1){
            return ['flag'=>false, 'msg'=>'您还没有登录'];
        }
        $score = MemberScore::getScore($lkt_uid);//获取用户当前试豆数

        if($score < 100){
            return ['flag'=>false, 'msg'=>'您的试豆不足100，快去赚取试豆再来吧'];
        }
        $username = MemberInfo::getUserNameByMemberId($lkt_uid);
        $model = new EggsGame();
        $result = $model->lottery($lkt_uid, $username, $score);

        if($result['flag']){
            MemberScore::addScore(-100, '每次砸金蛋消耗100试豆', $lkt_uid, 0);
            if(isset($result['prize']) && $result['prize'] > 0){
                MemberScore::addScore($result['prize'], '砸金蛋获得'.$result['prize'].'试豆', $lkt_uid, 0);
            }
            return ['flag'=>true, 'msg'=>$result['msg'], 'prize'=>$result['prize']];
        }
        else{
            return ['flag'=>false, 'msg'=>$result['msg']];
        }
    }

}